package com.trembear.ucaroauth.config;

import com.trembear.ucaroauth.service.CustomUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

/**
 * @author Junwei.Xiong
 * @description:
 * @create 2018-07-12 9:40
 */
@Configuration
@EnableWebSecurity
public class SecurityWebConfig extends WebSecurityConfigurerAdapter {
//    @Autowired
//    CustomUserService customUserService;
   @Override
   protected void configure(HttpSecurity http) throws Exception {
       http
           .authorizeRequests()
               .antMatchers("/login","/public","/failure")
               .permitAll()
               .anyRequest()
               .authenticated()
               .and()
           .formLogin()
               .loginPage("/login")
               .defaultSuccessUrl("/success")
               .successForwardUrl("/success")
               .failureForwardUrl("/failure")
               .and()
           .logout()
               .permitAll();
       http.csrf().disable();
   }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserService());
    }
    @Bean
    UserDetailsService customUserService() {
        return new CustomUserService();
    }
}
